 /* eslint-disable */
import {setStore,getStore,removeStore} from '../utils/storage'
import {
    saveOpenPage,
    delOpenPage,
    saveCurrentPage,
    addCachePage,
    clearOpenPage,
    saveUserInfo,
    saveAdEditId,
    saveSaveDomain,
    saveAdCopy,
    saveWarehouse,
} from './mutation-types.js'

export default {
    [saveOpenPage](state, page) {
        if(/(\/[a-zA-Z0-9]*)(\/*.*)/.test(page)){
            page = RegExp.$1
        }else{
            return
        }
        !state.pageOpenedList.includes(page) && state.pageOpenedList.push(page);
        state.currentPage = page;
    },
    [delOpenPage](state, page) {
        let index = state.pageOpenedList.indexOf(page)
        if(state.pageOpenedList.length > 1 && state.pageOpenedList.includes(page)){
            state.pageOpenedList.splice(index,1);
        }
        state.cachePage.forEach((item, index) => {
            if (item === page.slice(1)) {
                state.cachePage.splice(index, 1);
            }
        });
    },
    [saveCurrentPage](state, page) {
        state.currentPage = page;
    },
    [addCachePage](state, name) {
        state.cachePage.push(name)
    },
    [clearOpenPage](state, name) {
        state.pageOpenedList = []
        state.cachePage = []
    },
    [saveUserInfo](state, data) {
        state.userInfo.token = data.token;
        state.userInfo.permission = data.permission;
        state.userInfo.userType = data.userType;
        setStore('userInfo',JSON.stringify(data));
        setStore('token',data.token);
        setStore('userType',data.userType);
    },
    [saveAdEditId](state, data) {
        state.adEditId = data
    },
    [saveSaveDomain](state, data) {
        state.saveDomain = data
    },
    [saveAdCopy](state, data) {
        state.adCopy = data || false
    },
    [saveWarehouse](state, data) {
        state.warehouseOptions = data
    },
}
